/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.dspl.validate;

import com.google.common.collect.ImmutableList;
import com.google.dspl.model.Concept;
import com.google.dspl.model.Dataset;
import com.google.dspl.model.Slice;
import com.google.dspl.model.Table;
import com.google.dspl.model.Table.Column;
import com.google.dspl.model.Topic;
import com.google.dspl.validate.validator.ColumnIdValidator;
import com.google.dspl.validate.validator.ColumnValidator;
import com.google.dspl.validate.validator.ConceptValidator;
import com.google.dspl.validate.validator.DatasetConceptMappingsValidator;
import com.google.dspl.validate.validator.DatasetDimensionsValidator;
import com.google.dspl.validate.validator.GlobalUniqueIdValidator;
import com.google.dspl.validate.validator.SimpleDatasetValidator;
import com.google.dspl.validate.validator.SliceValidator;
import com.google.dspl.validate.validator.TableValidator;
import com.google.dspl.validate.validator.Validator;

import java.util.List;

/**
 * @author Shardul Deo
 */
public final class DatasetValidationBuilder {

  private final ImmutableList.Builder<Validator<Dataset>> datasetValidators =
      ImmutableList.builder();
  private final ImmutableList.Builder<Validator<Topic>> topicValidators = ImmutableList.builder();
  private final ImmutableList.Builder<Validator<Concept>> conceptValidators =
      ImmutableList.builder();
  private final ImmutableList.Builder<Validator<Slice>> sliceValidators = ImmutableList.builder();
  private final ImmutableList.Builder<Validator<Table>> tableValidators = ImmutableList.builder();

  public DatasetValidationBuilder withDefaultValidators() {
    datasetValidators.add(new SimpleDatasetValidator());
    datasetValidators.add(new DatasetConceptMappingsValidator());
    datasetValidators.add(new GlobalUniqueIdValidator());
    datasetValidators.add(new DatasetDimensionsValidator());

    conceptValidators.add(new ConceptValidator());
    sliceValidators.add(new SliceValidator());
    tableValidators.add(new TableValidator(new TableValidator.ColumnValidatorsFactory() {
      @Override
      public List<Validator<Column>> create(Table table) {
        return ImmutableList.of(new ColumnIdValidator(table), new ColumnValidator(table));
      }
    }));

    return this;
  }

  public DatasetValidationBuilder withCustomDatasetValidators(
      Iterable<? extends Validator<Dataset>> validators) {
    datasetValidators.addAll(validators);
    return this;
  }

  public DatasetValidationBuilder withCustomTopicValidators(
      Iterable<? extends Validator<Topic>> validators) {
    topicValidators.addAll(validators);
    return this;
  }

  public DatasetValidationBuilder withCustomConceptValidators(
      Iterable<? extends Validator<Concept>> validators) {
    conceptValidators.addAll(validators);
    return this;
  }

  public DatasetValidationBuilder withCustomSliceValidators(
      Iterable<? extends Validator<Slice>> validators) {
    sliceValidators.addAll(validators);
    return this;
  }

  public DatasetValidationBuilder withCustomTableValidators(
      Iterable<? extends Validator<Table>> validators) {
    tableValidators.addAll(validators);
    return this;
  }

  public DatasetValidation build() {
    return new DatasetValidation(
        datasetValidators.build(),
        topicValidators.build(),
        conceptValidators.build(),
        sliceValidators.build(),
        tableValidators.build());
  }
}
